package com.niit.springbootshop.controller;

import com.niit.springbootshop.dao.CategoryMapper;
import com.niit.springbootshop.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @program: springboot-demo
 * @description:
 * @author: hanliang
 * @create: 2021-05-06 14:31
 **/

@Controller // 控制器类
public class HelloController {

    // 注入：
    @Autowired  // 将Mybatis接口注入到Controller
    CategoryMapper categoryMapper;

    @RequestMapping("/hello")
    public String goHello(ModelMap map){
        List<Category> list = categoryMapper.findAll();

        for(Category c: list){
            System.out.println(c.getCid()+"--"+c.getCname());
        }

        map.put("clist",list);// 将数据回传到界面

        return "hello"; // 跳转到 classpath:/templates/ + hello + .html
    }

    @RequestMapping("/hello2")
    public ModelAndView goHello(){ // ModelAndView 可以同时返回界面和数据

        ModelAndView mv = new ModelAndView();

        List<Category> list = categoryMapper.findAll();

        mv.setViewName("hello"); // view 设置界面名称
        mv.addObject("categoryList",list);
        mv.addObject("username","韩亮");

        return mv;
    }
}
